package operations;

import entity.Student;
import entity.StudentList;

import java.util.Scanner;

// 修改学生信息
public class Modify implements IOperation {

    @Override
    public Student work() {
        Search search = new Search();
        // 先查找到需要删除的学生
        Student student = search.work();

        Scanner in = new Scanner(System.in);
        if (student != null) {
            while (true) {
                System.out.println("是否要修改该学生信息? (y/n)");
                String check = in.next();
                if ("y".equals(check) || "Y".equals(check)) {
                    StudentList.getInstance().remove(student);
                    System.out.println("请输入学号: ");
                    student.setId(in.next());
                    // 查询有没有重复添加
                    for (Student student1 : StudentList.getInstance()) {
                        if (student1.getId().equals(student.getId())) {
                            System.out.println("该学号已经存在, 不能重复添加!");
                            return null;
                        }
                    }
                    System.out.println("请输入姓名: ");
                    student.setName(in.next());
                    System.out.println("请输入年龄: ");
                    student.setAge(in.nextInt());
                    StudentList.getInstance().add(student);
                    System.out.println("修改成功, 修改后的信息为: \n" + student);
                    break;
                } else if ("n".equals(check) || "N".equals(check)) {
                    System.out.println("取消修改");
                    break;
                } else {
                    System.out.println("输入错误, 请重新输入");
                }
            }
        }
        return null;
    }
}
